<?php

namespace App\Admin\Controllers;

use App\Model\Setting;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SettingController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '系统参数';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Setting);

        $grid->column('type', '参数类型')->display(function ($value){
            switch ($value) {
                case Setting::TYPE_SHOPPING_PRICE:
                    return '运费';
                case Setting::TYPE_LOGO:
                    return 'Logo';
                case Setting::TYPE_BANNER:
                    return '首页Banner';
            }
        });
        $grid->column('value', '参数值');

        $grid->disableCreateButton()
            ->disableColumnSelector()
            ->disableRowSelector()
            ->disablePagination()
            ->disableFilter()
            ->disableExport();

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
        });

        return $grid;
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Setting);

        $params = request()->route()->parameters();

        $setting = Setting::where('id', $params['setting'])->first();

        switch ($setting->type) {
            case Setting::TYPE_SHOPPING_PRICE:
                $form->text('value', '值');
                break;
            case Setting::TYPE_LOGO:
            case Setting::TYPE_BANNER:
                $form->image('value', '值')->uniqueName();

        }

        $form->footer(function (/** @var Form\Footer $footer */$footer) {
            $footer->disableReset()
                ->disableViewCheck()
                ->disableEditingCheck()
                ->disableCreatingCheck();
        });

        $form->header(function (/** @var Form\Tools $header */$header){
            $header->disableView();
            $header->disableDelete();
        });

        return $form;
    }
}
